import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/',
    redirect: '/dashboard'
  },
  {
    path: '/dashboard',
    name: 'dashboard',
    component: () => import('../views/Dashboard.vue')
  },
  {
    path: '/users',
    name: 'users',
    component: () => import('../views/UserManage.vue')
  },
  {
    path: '/products',
    name: 'products',
    component: () => import('../views/ProductManage.vue')
  },
  {
    path: '/orders',
    name: 'orders',
    component: () => import('../views/OrderManage.vue')
  },
  {
    path: '/cart',
    name: 'cart',
    component: () => import('../views/CartManage.vue')
  },
  {
    path: '/categories',
    name: 'categories',
    component: () => import('../views/CategoryManage.vue')
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 路由守卫：未登录跳转到登录页
router.beforeEach((to, from, next) => {
  // const token = localStorage.getItem('token')
  // if (to.path !== '/login' && !token) {
  //   next('/login')
  // } else if (to.path === '/login' && token) {
  //   next('/dashboard')
  // } else {
  //   next()
  // }

  // 方便开发，暂时放开验证
  next()
})

export default router